﻿using System;
using System.Collections.Generic;
using Microsoft.SqlServer.Management.UI.VSIntegration.ObjectExplorer;
using System.Windows.Forms;
using Microsoft.SqlServer.Management.UI.VSIntegration;
using System.Text.RegularExpressions;

namespace SSMS_Scripter
{
    /// <summary>
    /// Represents the menu item added for processing options
    /// </summary>
    public class ColumnMenuItem : ToolsMenuItemBase, IWinformsMenuHandler
    {
        private string _ColumnName;
        #region Fields
        public string database;

        #endregion
        private string _TableName;
        private string _SchemaName;
        private string _DatabaseName;

        /// <summary>
        /// Constructor for process menu item
        /// </summary>
        /// <param name="databaseName"></param>
        /// <param name="tableName"></param>
        /// <param name="schemaName"></param>
        /// <param name="columnName"></param>
        /// <param name="cube">True if cube options should be displayed</param>
        /// <param name="dim">True if dimension options should be displayed</param>
        public ColumnMenuItem(string databaseName, string tableName, string schemaName, string columnName)
        {
            SetObjectNames(databaseName, tableName, schemaName, columnName);
            this.Text = "";
        }

        public void GetDatabaseInfo()
        {
            database = GetDBName(this.Parent.Context);
        }

        protected override void Invoke()
        {
        }

        /// <summary>
        /// Handles duplication
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new ColumnMenuItem(_DatabaseName, _TableName, _SchemaName, _ColumnName);
        }

        public void SetObjectNames(string databaseName, string tableName, string schemaName, string columnName)
        {
            _DatabaseName = databaseName;
            _ColumnName = columnName;
            _SchemaName = schemaName;
            _TableName = tableName;
        }
        #region IWinformsMenuHandler Members

        /// <summary>
        /// Returns list of items in context menu
        /// </summary>
        /// <returns></returns>
        public System.Windows.Forms.ToolStripItem[] GetMenuItems()
        {
            ToolStripMenuItem item = new ToolStripMenuItem("Group by");
            item.Click += ColumnGroup_Click;
            return new ToolStripItem[] { item };
        }

        void ColumnGroup_Click(object sender, EventArgs e)
        {
            string query = "SELECT\n\t[{0}],\n\tCOUNT(*) as [Count],\n\t" +
                "CONVERT(decimal(5,2), COUNT(*) * 100. / MAX(t.[Total])) [Pct]\n\nFROM [{1}].[{2}]\n" +
                "CROSS JOIN (SELECT COUNT(*) [Total] FROM [{1}].[{2}]) t\n" +
                "GROUP BY [{0}]\nORDER BY COUNT(*) DESC";

            query = string.Format(query, _ColumnName, _SchemaName, _TableName);
            Connect.LoadSQLDocument(query);
        }

        #endregion

        /// <summary>
        /// Returns match for database based on current context
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        protected string GetDBName(string context)
        {
            Regex dbRegex = new Regex(@"^Server\[[^\]]*\]/Database\[@ID='(?<Database>[^']*)'\]");
            return dbRegex.Match(context).Groups["Database"].Value;
        }

    }
}
